//
//  TopToolView.swift
//  BookNew
//
//  Created by iMac on 2021/7/22.
//  Copyright © 2021 iMac. All rights reserved.
//

import SwiftUI

struct TopToolView: View {
    class ToolModel: ObservableObject {
        @Published var urlString: String = ""
        @Published var isShow: Bool = true
        @Published var isRead: Bool = false
    }
    
    @StateObject var model: ToolModel
    
    @State private var height: CGFloat = 40
    
    @State private var inputStr: String = ""
    
    var body: some View {
        VStack(content: {
            HStack(alignment: .center, spacing: 10, content: {
                Text("笔趣阁").padding(10).foregroundColor(.orange).onTapGesture {
                    print("m.biquduu.com")
                    self.model.urlString = "https://m.biquduu.com/"
                }
                Text("顶点").padding(10).foregroundColor(.orange).onTapGesture {
                    model.urlString = "https://m.booktxt.net/"
                }
                Text("八零电子书").padding(10).foregroundColor(.orange).onTapGesture {
                    model.urlString = "https://m.80xs.la/"
                }
                Text("收藏夹").padding(10).foregroundColor(.orange)
                Image(systemName: "star").padding(10).foregroundColor(.orange)
                Image(systemName: "magnifyingglass").padding(10).foregroundColor(.orange)
                    .onTapGesture {
                        let h: CGFloat = height == 40 ?80:40
                        height = h;
                    }
                Text("Hide").padding(10).foregroundColor(.orange).onTapGesture {
                    height = 0
                    model.isShow = false
                }
                Text("Read").padding(10).foregroundColor(.orange).onTapGesture {
                    model.isRead.toggle()
                }
            })
            if height == 80 {
                TextField("input url", text: $inputStr, onCommit: {
                    model.urlString = inputStr
                })
                .padding(EdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 20))
                .frame(width: 300, height: 30, alignment: .center)
                .overlay(
                    RoundedRectangle(cornerRadius: 20)
                        .stroke(Color.gray, lineWidth: 2)
                )
            }
            
        }).frame(height: height).opacity(Double(height)/40)
        .onReceive(self.model.$isShow, perform: { show in
            print("isShow: \(show)")
            if (show){
                height = 40
            }
        })
    }
    
}

struct TopToolView_Previews: PreviewProvider {
    static var previews: some View {
        let model = TopToolView.ToolModel()
        TopToolView(model: model)
    }
}
